package com.wanbatv.wangwangba;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.wanbatv.wangwangba.util.BitmapCache;
import com.wanbatv.wangwangba.util.Util;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 俞亚楠 on 2015/10/29.
 */
public class ChengzhangrijiNeirongActivity extends FragmentActivity {

    private ViewPager mViewPager = null;
    private NeirongAdapter mAdapter = null;

    private RequestQueue mQueue = null;
    private ImageLoader mImageLoader = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chengzhangriji_neirong);

        mQueue = Volley.newRequestQueue(getApplicationContext());
        mImageLoader = new ImageLoader(mQueue, new BitmapCache());

        ArrayList<String> urls = getIntent().getStringArrayListExtra("urls");
        final ArrayList<String> urls2 = getIntent().getStringArrayListExtra("urls2");
        final ArrayList<String> urls3 = getIntent().getStringArrayListExtra("urls3");
        int position = getIntent().getIntExtra("position", 0);
        mViewPager = (ViewPager) findViewById(R.id.neirongPager);
        mAdapter = new NeirongAdapter(getSupportFragmentManager(), urls, urls2, urls3);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(position);
        mViewPager.setOffscreenPageLimit(0);
    }

    private class NeirongAdapter extends FragmentPagerAdapter {

        private List<String> mUrlList = null;
        private List<String> mTypeList = null;
        private List<String> mCoverList = null;

        private Fragment[] mFragments = null;

        public NeirongAdapter(FragmentManager fm, List<String> urls, List<String> types, List<String> covers) {
            super(fm);
            mUrlList = urls;
            mTypeList = types;
            mCoverList = covers;

            final int len = mUrlList.size();
            //仅仅初始化fragment数组的长度，
            //fragment的实例在getItem()的时候根据内容的类型延迟初始
            mFragments = new Fragment[len];
        }

        @Override
        public Fragment getItem(int position) {
            if (mFragments[position] == null) {
                Fragment f = null;
                if ("img".equals(mTypeList.get(position))) {
                    f = ChengzhangrijiImageFragment.newInstance(mUrlList.get(position));
                    ((ChengzhangrijiImageFragment) f).setImageLoader(mImageLoader);
                } else {
                    f = ChengzhangrijiVideoFragment.newInstance(
                            mUrlList.get(position), mCoverList.get(position));
                    ((ChengzhangrijiVideoFragment) f).setImageLoader(mImageLoader);
                }
                mFragments[position] = f;
            }
            return mFragments[position];
        }


        @Override
        public int getCount() {
            return mFragments.length;
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }

    private String buildVideoUrl(String url) {
        return "http://cdn.wanbatv.com" + url + Util.encryptUrl(url);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1000) {
            if (resultCode == RESULT_OK) {
                int keyCode = data.getIntExtra("key_code", 0);
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        if (mViewPager.getCurrentItem() > 0) {
                            mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1);
                        }
                        break;
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        if (mViewPager.getCurrentItem() < mAdapter.getCount() - 1) {
                            mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
                        }
                        break;
                }
            }
        }
    }
}
